package com.wl.mr.comment.step3;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class CommentOutputFormat extends FileOutputFormat<CommentBean, NullWritable> {

    @Override
    public RecordWriter<CommentBean, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
        //获得conf
        Configuration conf = job.getConfiguration();
        //获得文件系统
        FileSystem fileSystem = FileSystem.get(conf);
        //获得output的路径
        String outputPath = conf.get("mapreduce.output.fileoutputformat.outputdir");

        String goodPath = outputPath + "\\good\\good.log";
        String commentPath = outputPath + "\\comment\\comment.log";
        String badPath = outputPath + "\\bad\\bad.log";

        int id = job.getTaskAttemptID().getTaskID().getId();
        FSDataOutputStream goodOut = null;
        FSDataOutputStream commentOut = null;
        FSDataOutputStream badOut = null;
        if(id == 0 ){
            fileSystem.create(new Path(goodPath));
        }else if(id == 1){
            fileSystem.create(new Path(commentPath));
        }else{
            fileSystem.create(new Path(badPath));
        }


        return new CommentRecordWrite(goodOut,commentOut,badOut);

    }
}
